package com.yys.excel.yysexcel.controller;


import cn.hutool.core.util.StrUtil;
import com.yys.excel.yysexcel.common.result.R;
import com.yys.excel.yysexcel.entity.entityBo.ImportExportParamBo;
import com.yys.excel.yysexcel.service.ImportExportService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 导入
 *
 * @author lys
 * @date 2022/12/5
 */
@RestController
@RequestMapping("/import")
@RequiredArgsConstructor
@CrossOrigin
public class ImportController {
    private final ImportExportService importExportService;

    /**
     * 将文件内容导入到数据库中
     *
     * @param file 文件
     */
    @PostMapping("/importByFile")
    public R importByFile(String templateId, MultipartFile file) {
        if (StrUtil.isBlank(templateId)) {
            return R.fail("模板为空");
        }
        ImportExportParamBo importData = ImportExportParamBo.builder().templateId(templateId).build();
        importData.setFile(file);
        String id = importExportService.importByFile(importData);
        return R.ok(id);
    }
}
